{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from malaya.num2word import to_cardinal, to_ordinal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "def money(x):\n",
    "    try:\n",
    "        if (\n",
    "            re.match('^\\$', x)\n",
    "            or x.lower().endswith('dollar')\n",
    "            or x.lower().endswith('cent')\n",
    "        ):\n",
    "            x = x.lower()\n",
    "            if not x.startswith('$') and x.endswith('cent'):\n",
    "                cent = True\n",
    "            else:\n",
    "                cent = False\n",
    "            x = x.replace('$', '').replace('dollar', '').replace('cent', '')\n",
    "            x = re.sub(r'[ ]+', ' ', x).strip()\n",
    "            x, n = re.split(\"(\\d+(?:[\\.,']\\d+)?)\", x)[1:]\n",
    "            print(x, n)\n",
    "            x = re.sub(',', '', x, count = 10)\n",
    "            labels = []\n",
    "            for c in n:\n",
    "                if re.match('.*(M|m)$', c):\n",
    "                    labels.append(1e6)\n",
    "                elif re.match('.*(b|B)$', c):\n",
    "                    labels.append(1e9)\n",
    "                elif re.match('.*(k|K)$', c):\n",
    "                    labels.append(1e3)\n",
    "            \n",
    "            if cent:\n",
    "                x = float(x)\n",
    "                x = x / 100\n",
    "            for l in labels:\n",
    "                x = float(x)\n",
    "                x = x * l\n",
    "                \n",
    "            x, c = combine_with_cent(x, currency ='$', currency_end = 'dollar', cent ='cent')\n",
    "                \n",
    "            return re.sub(r'[ ]+', ' ', x.lower()).strip(), c\n",
    "\n",
    "        elif (\n",
    "            re.match('^US', x)\n",
    "            or x.lower().endswith('dollar')\n",
    "            or x.lower().endswith('cent')\n",
    "            or x.lower().endswith('usd')\n",
    "        ):\n",
    "            x = x.lower()\n",
    "            if not x.startswith('US') and x.endswith('cent'):\n",
    "                cent = True\n",
    "            else:\n",
    "                cent = False\n",
    "            x = x.replace('US', '').replace('usd', '').replace('dollar', '').replace('cent', '')\n",
    "            x = re.sub(r'[ ]+', ' ', x).strip()\n",
    "            x, n = re.split(\"(\\d+(?:[\\.,']\\d+)?)\", x)[1:]\n",
    "            print(x, n)\n",
    "            x = re.sub(',', '', x, count = 10)\n",
    "            labels = []\n",
    "            for c in n:\n",
    "                if re.match('.*(M|m)$', c):\n",
    "                    labels.append(1e6)\n",
    "                elif re.match('.*(b|B)$', c):\n",
    "                    labels.append(1e9)\n",
    "                elif re.match('.*(k|K)$', c):\n",
    "                    labels.append(1e3)\n",
    "            \n",
    "            if cent:\n",
    "                x = float(x)\n",
    "                x = x / 100\n",
    "            for l in labels:\n",
    "                x = float(x)\n",
    "                x = x * l\n",
    "            \n",
    "            x, c = combine_with_cent(x, currency ='$', currency_end = 'dollar', cent ='cent')\n",
    "                \n",
    "            return re.sub(r'[ ]+', ' ', x.lower()).strip(), c\n",
    "\n",
    "        elif (\n",
    "            re.match('^\\£', x)\n",
    "            or x.lower().endswith('pound')\n",
    "            or x.lower().endswith('penny')\n",
    "        ):\n",
    "            x = x.lower()\n",
    "            if not x.startswith('£') and x.endswith('penny'):\n",
    "                cent = True\n",
    "            else:\n",
    "                cent = False\n",
    "            x = x.replace('£', '').replace('pound', '').replace('penny', '')\n",
    "            x = re.sub(r'[ ]+', ' ', x).strip()\n",
    "            x, n = re.split(\"(\\d+(?:[\\.,']\\d+)?)\", x)[1:]\n",
    "            x = re.sub(',', '', x, count = 10)\n",
    "            labels = []\n",
    "            for c in n:\n",
    "                if re.match('.*(M|m)$', c):\n",
    "                    labels.append(1e6)\n",
    "                elif re.match('.*(b|B)$', c):\n",
    "                    labels.append(1e9)\n",
    "                elif re.match('.*(k|K)$', c):\n",
    "                    labels.append(1e3)\n",
    "            \n",
    "            if cent:\n",
    "                x = float(x)\n",
    "                x = x / 100\n",
    "            for l in labels:\n",
    "                x = float(x)\n",
    "                x = x * l\n",
    "                \n",
    "            x, c = combine_with_cent(x, currency ='£', currency_end = 'pound', cent ='cent')\n",
    "            return re.sub(r'[ ]+', ' ', x.lower()).strip(), c\n",
    "\n",
    "        elif (\n",
    "            re.match('^\\€', x)\n",
    "            or x.lower().endswith('euro')\n",
    "            or x.lower().endswith('cent')\n",
    "        ):\n",
    "            x = x.lower()\n",
    "            if not x.startswith('€') and x.endswith('cent'):\n",
    "                cent = True\n",
    "            else:\n",
    "                cent = False\n",
    "            x = x.replace('€', '').replace('euro', '').replace('cent', '')\n",
    "            x = re.sub(r'[ ]+', ' ', x).strip()\n",
    "            x, n = re.split(\"(\\d+(?:[\\.,']\\d+)?)\", x)[1:]\n",
    "            x = re.sub(',', '', x, count = 10)\n",
    "            labels = []\n",
    "            for c in n:\n",
    "                if re.match('.*(M|m)$', c):\n",
    "                    labels.append(1e6)\n",
    "                elif re.match('.*(b|B)$', c):\n",
    "                    labels.append(1e9)\n",
    "                elif re.match('.*(k|K)$', c):\n",
    "                    labels.append(1e3)\n",
    "\n",
    "            x = float(x)\n",
    "            if cent:\n",
    "                x = x / 100\n",
    "            for l in labels:\n",
    "                x = x * l\n",
    "                \n",
    "            text = split_currency(str(x))\n",
    "            c = '€%s' % (str(x))\n",
    "            x = '%s euro' % (text[0])\n",
    "            if len(text) == 2:\n",
    "                x = '%s %s cent' % (x, text[1])\n",
    "            return re.sub(r'[ ]+', ' ', x.lower()).strip(), c\n",
    "\n",
    "        elif (\n",
    "            re.match('^RM', x)\n",
    "            or re.match('^rm', x)\n",
    "            or x.lower().endswith('ringgit')\n",
    "            or x.lower().endswith('sen')\n",
    "        ):\n",
    "            x = x.lower()\n",
    "            if not x.startswith('rm') and x.endswith('sen'):\n",
    "                cent = True\n",
    "            else:\n",
    "                cent = False\n",
    "            x = x.replace('rm', '').replace('ringgit', '').replace('sen', '')\n",
    "            x = re.sub(r'[ ]+', ' ', x).strip()\n",
    "            x, n = re.split(\"(\\d+(?:[\\.,']\\d+)?)\", x)[1:]\n",
    "            x = re.sub(',', '', x, count = 10)\n",
    "            labels = []\n",
    "            for c in n:\n",
    "                if re.match('.*(M|m)$', c):\n",
    "                    labels.append(1e6)\n",
    "                elif re.match('.*(b|B)$', c):\n",
    "                    labels.append(1e9)\n",
    "                elif re.match('.*(k|K)$', c):\n",
    "                    labels.append(1e3)\n",
    "                    \n",
    "            if cent:\n",
    "                x = float(x)\n",
    "                x = x / 100\n",
    "            for l in labels:\n",
    "                x = float(x)\n",
    "                x = x * l\n",
    "                \n",
    "            x, c = combine_with_cent(x)\n",
    "            return re.sub(r'[ ]+', ' ', x.lower()).strip(), c\n",
    "        return x, None\n",
    "\n",
    "    except Exception as e:\n",
    "        print(e)\n",
    "        return x, None\n",
    "\n",
    "def combine_with_cent(x, currency = 'RM', currency_end = 'ringgit', cent = 'sen'):\n",
    "    text = split_currency(str(x))\n",
    "    c = '%s%s' % (currency, str(x))\n",
    "    if text[0] != 'kosong':\n",
    "        x = '%s %s' % (text[0], currency_end)\n",
    "    else:\n",
    "        x = ''\n",
    "    if len(text) == 2:\n",
    "        if text[1] != 'kosong':\n",
    "            x = '%s %s %s' % (x, text[1], cent)\n",
    "    return x, c\n",
    "    \n",
    "def cardinal(x):\n",
    "    try:\n",
    "        if re.match('.*[A-Za-z]+.*', x):\n",
    "            return x\n",
    "        x = re.sub(',', '', x, count = 10)\n",
    "\n",
    "        if re.match('.+\\..*', x):\n",
    "            x = to_cardinal(float(x))\n",
    "        elif re.match('\\..*', x):\n",
    "            x = to_cardinal(float(x))\n",
    "        else:\n",
    "            x = to_cardinal(int(x))\n",
    "        x = re.sub('-', ' ', x, count = 10)\n",
    "        return x\n",
    "    except:\n",
    "        return x\n",
    "    \n",
    "def split_currency(x):\n",
    "    results = []\n",
    "    for no, u in enumerate(x.split('.')):\n",
    "        if no and len(u) == 1:\n",
    "            u = u + '0'\n",
    "        results.append(cardinal(u))\n",
    "    return results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10.5 \n",
      "['sepuluh', 'lima puluh']\n",
      "sepuluh ringgit lima puluh sen\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "('sepuluh ringgit lima puluh sen', 'RM10.5')"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "money('RM 10.5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10.400000001 m\n",
      "['sepuluh juta empat ratus ribu', 'satu']\n",
      "sepuluh juta empat ratus ribu ringgit satu sen\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "('sepuluh juta empat ratus ribu ringgit satu sen', 'RM10400000.001')"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "money('rm10.4m')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from malaya.num2word import to_cardinal, to_ordinal, split_by_koma, split_by_3, spell, puluh, ratus, BASE, join"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "to_cardinal(11400.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def spell(blocks):\n",
    "    word_blocks = ()\n",
    "    first_block = blocks[0]\n",
    "    if len(first_block[0]) == 1:\n",
    "        if first_block[0] == '0':\n",
    "            spelling = ['kosong']\n",
    "        else:\n",
    "            spelling = BASE[int(first_block[0])]\n",
    "    elif len(first_block[0]) == 2:\n",
    "        spelling = puluh(first_block[0])\n",
    "    else:\n",
    "        spelling = ratus(first_block[0][0]) + puluh(first_block[0][1:3])\n",
    "    word_blocks += ((first_block[0], spelling),)\n",
    "    for block in blocks[1:]:\n",
    "        spelling = ratus(block[0][0]) + puluh(block[0][1:3])\n",
    "        block += (spelling,)\n",
    "        word_blocks += (block,)\n",
    "    return word_blocks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def split_by_3(number):\n",
    "    blocks = ()\n",
    "    length = len(number)\n",
    "    if length < 3:\n",
    "        blocks += ((number,),)\n",
    "    else:\n",
    "        len_of_first_block = length % 3\n",
    "        if len_of_first_block > 0:\n",
    "            blocks += ((number[0:len_of_first_block],),)\n",
    "        for i in range(len_of_first_block, length, 3):\n",
    "            blocks += ((number[i : i + 3],),)\n",
    "    return blocks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "split_by_3('2000000')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "spell(split_by_3('20000'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "join(spell(split_by_3(n[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "split_by_3('10400')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "puluh('20')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "split_by_koma(abs(20400.0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "money('RM10.4K')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "money('1922 sen')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "money('rm15.005')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "money('rm 10.5 sen')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "money('5411sen')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "money('rm50.1M')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "money('rm50.2k')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
